using Microsoft.Office.Interop.Excel;
using System;
using System.Data;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationOrderShipmentMessageForm
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private Range range;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationOrderShipmentMessageForm(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeDataToExcel(string templetFile, System.Data.DataTable dt, string sCode, string sOperatorName)
		{
			this.OpenExcel(templetFile);
			this.MakeDataToExcel_1(dt, sCode, sOperatorName, 11);
		}

		private void MakeDataToExcel_1(System.Data.DataTable dt, string sCode, string sOperatorName, int nColumnCountOfPage)
		{
			int num = 0;
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				if (Convert.ToBoolean(dt.Rows[i][0]))
				{
					num++;
				}
				else
				{
					dt.Rows[i].Delete();
				}
			}
			dt.AcceptChanges();
			int num2 = num % nColumnCountOfPage;
			int num3;
			if (num2 == 0)
			{
				num3 = num / nColumnCountOfPage;
			}
			else
			{
				num3 = num / nColumnCountOfPage + 1;
			}
			try
			{
				for (int i = 0; i < num3 - 1; i++)
				{
					this.workSheet.Copy(this.missing, this.workBook.Worksheets[1]);
				}
				for (int i = 0; i < num3; i++)
				{
					this.workSheet = (Worksheet)this.workBook.Worksheets.get_Item(i + 1);
					this.workSheet.Name="Page-" + (i + 1).ToString();
				}
				this.workSheet = (Worksheet)this.workBook.Worksheets[1];
				this.workSheet.Activate();
				int num4 = 6;
				this.workSheet.Cells[3, 5]= sCode;
				this.workSheet.Cells[5, 5]= DateTime.Today.ToShortDateString();
				for (int i = 0; i < dt.Rows.Count; i++)
				{
					num4++;
					if (num4 > 7 + nColumnCountOfPage - 1)
					{
						this.workSheet = (Worksheet)this.workBook.Worksheets[this.workSheet.Index + 1];
						this.workSheet.Activate();
						num4 = 7;
						this.workSheet.Cells[3, 5]= sCode;
						this.workSheet.Cells[5, 5]= DateTime.Today.ToShortDateString();
					}
					this.workSheet.Cells[num4, 1]= dt.Rows[i][2].ToString();
					this.workSheet.Cells[num4, 2]= dt.Rows[i][3].ToString();
					this.workSheet.Cells[num4, 3]= dt.Rows[i][11].ToString();
					this.workSheet.Cells[num4, 4]= dt.Rows[i][7].ToString();
					this.workSheet.Cells[num4, 5]= dt.Rows[i][8].ToString();
					this.workSheet.Cells[num4, 6]= dt.Rows[i][12].ToString();
					if (num4 == 7 + nColumnCountOfPage - 1 || i == dt.Rows.Count - 1)
					{
						if (num4 > 6)
						{
							this.range = this.workSheet.get_Range(this.workSheet.Cells[6, 1], this.workSheet.Cells[num4, 6]);
                            this.range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = 1;
                            this.range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = 1;
                            this.range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = 1;
                            this.range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            this.range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = 1;
                            this.range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = 1;
                        }
						this.workSheet.Cells[num4 + 1, 1]= "客户签章:";
						this.workSheet.Cells[num4 + 1, 3]= "业务主管:";
						this.workSheet.Cells[num4 + 1, 5]= "制单：";
						this.workSheet.Cells[num4 + 1, 6]= sOperatorName;
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
